home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 8102 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.4 KB

  1. Path: newsfeeds.ans.net!philabs!usenet
  2. From: abf@philabs.research.philips.com (Andrew Feldman)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: callback function in a class
  5. Date: Wed, 14 Feb 1996 21:03:18 GMT
  6. Organization: Philips Laboratories, Briarcliff, NY 10510
  7. Distribution: inet
  8. Message-ID: <4ft814$hde@philabs.research.philips.com>
  9. References: <4fe0lk$cu3@aldebaran.sct.fr> <Pine.OSF.3.91.960214160359.19924A-100000@stio1>
  10. NNTP-Posting-Host: idrpc5.philabs.research.philips.com
  11. X-Newsreader: Forte Free Agent 1.0.82
  12.  
  13. Schmidt Carsten <i010@stio1> wrote:
  14.  
  15.  
  16. >Hi, everybody,
  17.  
  18. >Does anybody have an idea, how I can put a callback function in a class, 
  19. >look for the example:
  20.  
  21. >class X { 
  22. >  DWORD ThreadProc (..parms...);
  23. >  void StartIt (void);
  24. >...
  25. >};
  26.  
  27. >DWORD X::ThreadProc (...parms...)
  28. >{
  29. >   ....
  30. >}
  31.  
  32. >void X::StartIt (void)
  33. >{
  34. >  hThread = CreateThread (..., ThreadProc, ...);
  35. >}
  36.  
  37. >Don't worry about Threads, it is just an example. I try to find a 
  38. >solution for the general problem: How to use a callback-function in a class.
  39.  
  40. >I think some programmers know the problem.
  41.  
  42. >Ciao,
  43. >Carsten.
  44. >       ____________________________________________________________
  45. >      /                     Carsten SCHMIDT                       /
  46. >     /               student of computer science                 /
  47. >    /    Fachhochschule Wuerzburg-Schweinfurt-Aschaffenburg     /
  48. >   /       Friedenstr. 2/1715, 97072 Wuerzburg, Germany        /
  49. >  /      Tel: +49 931 800 56 07, Fax: +49 931 610 14 68       /
  50. > /            E-Mail: i010@stio1.fh-wuerzburg.de             /                             
  51. >/___________________________________________________________/
  52.  
  53.  
  54. For CreateThread purposes (actually in many other cases also) I'd do
  55. the following. Define a static member function or just a global
  56. function GenericThreadProc:
  57.  
  58. GenericThreadProc(X* x)
  59. { x->ThreadProc() }
  60.  
  61. ThreadProc can't have parameters, they should be passes in the data
  62. members of x. In the StartIt(), you do
  63.  
  64. hThread = CreateThread (..., GenericThreadProc, this ...);
  65.  
  66. Functions like CreateThread always have as their parameter not only a
  67. pointer to the callback function, but also a void* or char* pointer
  68. which is gonna be used as a parameter to this callback function.
  69.  
  70. You know, this is only the idea (which worked -- guaranteed). You
  71. should work out the details. For example, callback function is not
  72. supposed to be of type (*)(X*), so you may have to add additional
  73. parameters to its header and do some type conversions.
  74.  
  75. Andrew.
  76.  
  77.  
  78.